<?php

namespace App\Http\Controllers\Personal;
use App\Models\Common\CollArea;
use App\Models\Company\CompanyJob;
use App\Models\Personal\Resume;
use App\Models\Personal\ResumeSend;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Style;

class BrowseJobController extends CommonController{
    /**
     * 浏览职位记录
     * @param Request $request
     * @return mixed
     */
    public function browseJob(Request $request){
        $user = auth()->guard('personal')->user();
        //关键词搜索
        $keyword  = trim($request->keyword);
        //浏览职位
        $browse_job = DB::table('browse_job as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            ->join('company_users as t3', 't1.company_uid', '=', 't3.uid')
            ->join('company_job as t4', 't1.job_id', '=', 't4.id')
            ->select(
                't1.id','t1.created_at','t1.job_id','t1.com_id',
                't2.company_name','t2.region','t2.industry','t2.region',
                't3.last_login_time',
                't4.job_title'
            )
            ->where('t1.personal_uid',$user->uid)
            ->where(function ($query) use ($keyword)  {
                if(strlen($keyword) > 0){
                    $query->where('t2.company_name','like','%'.$keyword.'%');
                }
            })
            ->orderBy('created_at', 'desc')
            ->paginate(10);
        //dd($browse_job);
        //处理数组
        foreach($browse_job as $v){
            $v->job_title = str_limit($v->job_title,14);
            $v->company_name = str_limit($v->company_name,26);
            $v->region = str_limit(str_replace(',','',CollArea::getCityAddress($v->region)),18);
            $v->created_at = substr($v->created_at,0,10);
            $v->last_login_time = substr($v->last_login_time,0,16);
        }
        //分页分配参数 过滤空值
        $appends =  arrayFilter( array(
            'keyword' => $keyword
        ));
        return Style::view('personal.browseJob',compact('browse_job','appends'));
    }

    /**
     * 浏览职位批量投递
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function browseJobSend(Request $request){
        $user = auth()->guard('personal')->user();
        //判断有没有简历ID
        $resume = Resume::where('uid',$user->uid)->first(['id']);
        if($resume) {
            $input = [];
            //获取职位id
            $job_id = $request->input('selected_id.*');
            foreach ($job_id as $k => $v) {
                $ResumeSend = ResumeSend::where('job_id', $v)->where('personal_uid', $user->uid)->where('resume_id', $resume->id)
                    ->orderBy('updated_at', 'desc')->first(['id', 'updated_at']);
                if (count($ResumeSend) > 0) {
                    //判断是不是当天投递简历
                    if (strtotime($ResumeSend['updated_at']) + 24 * 3600 > time()) {
                        $input['updated_at'] = date('Y-m-d H:i:s', time());
                        ResumeSend::where('id', $ResumeSend['id'])->where('personal_uid', $user->uid)->update($input);
                    } else {
                        $CompanyJob = CompanyJob::where('id', $v)->first(['uid', 'com_id']);
                        $input['company_uid'] = $CompanyJob->uid;
                        $input['com_id'] = $CompanyJob->com_id;
                        $input['personal_uid'] = $user->uid;
                        $input['resume_id'] = $resume->id;
                        $input['job_id'] = $v;
                        //插入投递记录
                        ResumeSend::create($input);
                    }
                } else {
                    $CompanyJob = CompanyJob::where('id', $v)->first(['uid', 'com_id']);
                    $input['company_uid'] = $CompanyJob->uid;
                    $input['com_id'] = $CompanyJob->com_id;
                    $input['personal_uid'] = $user->uid;
                    $input['resume_id'] = $resume->id;
                    $input['job_id'] = $v;
                    //插入投递记录
                    ResumeSend::create($input);
                }
            }
            return redirect('personal/browse_job');
        }else{
            return redirect('personal/my_resume');
        }
    }
}